package com.building18.controller;

import com.alibaba.excel.EasyExcel;
import com.building18.handler.MyAssert;
import com.building18.pojo.Building;
import com.building18.pojo.House;
import com.building18.pojo.MyPageData;
import com.building18.pojo.Result;
import com.building18.pojo.dto.HouseDTO;
import com.building18.pojo.enums.Code;
import com.building18.pojo.vo.HouseVO;
import com.building18.pojo.vo.StateVO;
import com.building18.service.BuildingService;
import com.building18.service.HouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/house")
@Tag(name = "房屋管理", description = "房屋信息的增删改查接口")
public class HouseController {
    @Autowired
    private HouseService houseService;

    @GetMapping("/getHouseNoAndId")
    @Operation(summary ="查询所有房屋编号和id")
    public Result<?> getHouseNoAndId(){
        List<HouseVO> houseNoAndId = houseService.getHouseNoAndId();
        return Result.ok(houseNoAndId, Code.SELECT_SUCCESS.getMessage());
    }


    @PostMapping("/{pageNum}/{pageSize}")
    @Operation(summary ="查询楼宇所有信息")
    public Result<?> selectBuilding(
            @PathVariable int pageNum,
            @PathVariable int pageSize,
            @RequestBody(required = false)HouseDTO houseDTO
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<House> houses = houseService.selectHouse(houseDTO);
        PageInfo<House> buildingPageInfo = new PageInfo<>(houses);
        MyPageData myPageData=new MyPageData(buildingPageInfo.getTotal(),buildingPageInfo.getList());
        return Result.ok(myPageData, Code.SELECT_SUCCESS.getMessage());
    }

    @GetMapping("/getHouseStateNameAndId")
    @Operation(summary ="查询所有房屋状态名称和id")
    public Result<?> getHouseStateNameAndId() {
        List<StateVO> houseStateNameAndId = houseService.getHouseStateNameAndId();
        return Result.ok(houseStateNameAndId, Code.SELECT_SUCCESS.getMessage());
    }

    @PostMapping("/addHouse")
    @Operation(summary ="添加房屋")
    public Result<?> addHouse(@RequestBody House house) {
        Integer i=houseService.addHouse(house);
        MyAssert.error(i<=0, Code.INSERT_FAIL.getMessage());
        return Result.ok(Code.INSERT_SUCCESS.getMessage());
    }

    @DeleteMapping("/deleteHouse/{id}")
    @Operation(summary ="删除房屋")
    public Result<?> deleteHouse(@PathVariable Integer id) {
        Integer i=houseService.deleteHouse(id);
        MyAssert.error(i<=0, Code.DELETE_FAIL.getMessage());
        return Result.ok(Code.DELETE_SUCCESS.getMessage());
    }

    @PutMapping("/updateHouse")
    @Operation(summary ="更新房屋")
    public Result<?> updateHouse(@RequestBody House house) {
        Integer i=houseService.updateHouse(house);
        MyAssert.error(i<=0, Code.UPDATE_FAIL.getMessage());
        return Result.ok(Code.UPDATE_SUCCESS.getMessage());
    }



    @GetMapping("download")
    @Operation(summary ="下载房屋信息")
    public void download(HttpServletResponse response) throws IOException {

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        String fileName = URLEncoder.encode("房屋数据", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), House.class).sheet("模板").doWrite(houseService.selectHouse(new HouseDTO()));
    }


}
